﻿using Shared.Method;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;
using Shared.SoftMethod;
using Shared.Model;
using System.Text.RegularExpressions;

namespace QCTools
{
    public partial class FormQCTools : Form
    {
        //private bool _worked = false;

        private string _localIp, _modemIp;
        private int _localPort;

        private List<Device_Type> _listType = null;
        private ModemType _modemType;

        

        public FormQCTools()
        {
            InitializeComponent();
        }

        private void BobTest()
        {
            YcdTelnetSecond tel = new YcdTelnetSecond("192.168.1.1", "192.168.1.213", 34553);
            tel.Connect();

            BobBase.WriteReg(tel, 0x51, 0x7F, 0x04);
            var registers = BobBase.ReadReg(tel, 0x51, 0x80, 0xE8);
            BobBase.WriteReg(tel, 0x51, 0x7F, 0x05);
            registers = BobBase.ReadReg(tel, 0x51, 0x80, 0xE8);
            BobBase.WriteReg(tel, 0x51, 0x7F, 0x06);
            registers = BobBase.ReadReg(tel, 0x51, 0x80, 0xE8);
            BobBase.WriteReg(tel, 0x51, 0x7F, 0x03);
            registers = BobBase.ReadReg(tel, 0x51, 130, 210);


            tel.Send("exit");
            tel.Close();
        }

        private void FormQCTools_Load(object sender, EventArgs e)
        {
            //BobTest();
            //return;

            try
            {
                _localIp = AppSetting.GetValue("LocalIp1");
                _localPort = int.Parse(AppSetting.GetValue("LocalPort1"));
                _modemIp = AppSetting.GetValue("IpAddress");

                Device_Type typeW = new Device_Type();

                var selectTYpe = Client.MethodWcf.SelectDeviceTypeClient(typeW);
                if (selectTYpe.result)
                {
                    _listType = selectTYpe.value;
                }
                else
                {
                    MessageBox.Show("查询DeviceType失败");
                    Close();
                    return;
                }

                Working();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
                Close();
            }
        }

        //private void BobBase_OnReturnString(string obj)
        //{
        //    SoftShared.AppendText(this, this.richTextBoxMsg, obj);
        //}

        private void RichTextBoxMsg_TextChanged(object sender, EventArgs e)
        {
            richTextBoxMsg.SelectionStart = richTextBoxMsg.Text.Length;
            richTextBoxMsg.ScrollToCaret();
        }

        private void Working()
        {
            Task.Run(() =>
            {
                //DateTime date = DateTime.Now;
                //YcdTelnetSecond telnet2 = new YcdTelnetSecond("192.168.1.1", "192.168.1.213", 31837, 23);
                //bool con = telnet2.Connect();

                //byte[] reg = BobBase.ReadReg(telnet2, 0x51, 0x80, 0xa0);

                //telnet2.Send("exit");

                //telnet2.Close();

                //double runtime = (DateTime.Now - date).TotalSeconds;

                //MessageBox.Show($"运行时间：{runtime}");

                while (true)
                {
                    //判断光猫断开连接
                    SoftShared.AppendText(this, this.richTextBoxMsg, "等待断开光猫");

                    while (PingBaseClass.PingTest(_localIp, _modemIp) == true) Thread.Sleep(2000);

                    SoftShared.SetControl(this, this.richTextBoxMsg, string.Empty);
                    SoftShared.SetControl(this, this.labelState, "WAIT");

                    //PING通光猫
                    int pingOkTimes = 0;
                    for (int i = 0; i < 90; i++)
                    {
                        if (PingBaseClass.PingTest(_localIp, _modemIp))
                        {
                            pingOkTimes++;
                            if (pingOkTimes >= 4)
                                break;
                        }
                        else
                        {
                            pingOkTimes = 0;
                        }
                        Thread.Sleep(500);
                    }

                    if (pingOkTimes < 4) { SoftShared.AppendText(this, this.richTextBoxMsg, "未能PING通光猫"); continue; }

                    string mac = string.Empty;

                    //获取MAC地址
                    for (int i = 0; i < 3; i++)
                    {
                        if (string.IsNullOrEmpty(mac = ArpBaseClass.GetMacFromIp(_localIp, _modemIp)))
                        {
                            Thread.Sleep(1500);
                        }
                        else
                        {
                            break;
                        }

                    }

                    if (string.IsNullOrEmpty(mac)) { SoftShared.AppendText(this, this.richTextBoxMsg, "获取MAC地址失败"); continue; }

                    //WORKING
                    SoftShared.SetControl(this, this.labelState, "WORKING");
                    SoftShared.SetControl(this, this.labelMAC, mac);

                    Device_Data deviceData = null;

                    //获取device
                    Device_Data dataW = new Device_Data();
                    dataW.SetWhere();
                    dataW.MACAddr = mac;

                    var selectData = Client.MethodWcf.SelectDeviceDataClient(dataW);
                    if (selectData.result)
                    {
                        deviceData = selectData.value[0];
                    }
                    else
                    {
                        SoftShared.AppendText(this, this.richTextBoxMsg, "获取MAC数据失败");
                        MessageBox.Show("获取MAC数据失败");
                        continue;
                    }

                    //根据MAC对应的数据，确定使用什么方式开TELNET
                    if (deviceData.DeviceType.Contains("CMCC"))
                    {
                        //LoginCMCC();
                        SoftShared.LoginCMCC(_modemIp, _localIp, _localPort, ref portOffset, deviceData.HW_PROVINCE_NAME);

                        if (GetModemMessage("192.168.1.1:80/cgi-bin/abcdidfope94e0934jiewru8ew414.cgi", "success") == false)
                        {
                            SoftShared.SetControl(this, this.labelState, "FAIL", Color.Red);
                            SoftShared.AppendText(this, this.richTextBoxMsg, "开TELNET失败");
                            continue;
                        }
                    }
                    else if (deviceData.DeviceType.Contains("PT952"))
                    {
                        if (GetModemMessage("192.168.1.1:80/cgi-bin/abcdidfope94e0934jiewru8ew414.cgi", "success") == false)
                        {
                            SoftShared.SetControl(this, this.labelState, "FAIL", Color.Red);
                            SoftShared.AppendText(this, this.richTextBoxMsg, "开TELNET失败");
                            continue;
                        }
                    }
                    else
                    {
                        if (GetModemMessage("192.168.1.1:8080/cgi-bin/abcdidfope94e0934jiewru8ew414.cgi", "success") == false)
                        {
                            SoftShared.SetControl(this, this.labelState, "FAIL", Color.Red);
                            SoftShared.AppendText(this, this.richTextBoxMsg, "开TELNET失败");
                        }
                    }

                    //开TELNET
                    var tel = TelnetModem(deviceData);

                    if (tel == null)
                    {
                        SoftShared.SetControl(this, this.labelState, "FAIL", Color.Red);
                        SoftShared.AppendText(this, this.richTextBoxMsg, "TELNET失败");
                        continue;
                    }

                    //检查网速
                    bool speed = YcdTelnetMethod.CheckPortSpeed(tel, out string message);

                    SoftShared.AppendText(this,this.richTextBoxMsg,message);

                    if (speed == false)
                    {
                        SoftShared.AppendText(this, this.richTextBoxMsg, "端口网速检查失败");
                        SoftShared.SetControl(this, this.labelState, "FAIL", Color.Red);
                        CloseTelnet(tel);
                        continue;
                    }

                    //检查光功率
                    if (deviceData.PONType == "EPON")
                        tel.SendAndRecv("diag epon set force-laser-on state enable", "#", out message);
                    else
                        tel.SendAndRecv("diag gpon set tx-laser force-on", "#", out message);

                    SoftShared.AppendText(this, this.richTextBoxMsg, message);

                    SoftShared.AppendText(this, this.richTextBoxMsg, "\r你有30秒时间检查光功率是否正常");
                    SoftShared.SetControl(this, this.labelState, "检查光功率", Color.BlueViolet);

                    System.Threading.Thread.Sleep(30000);

                    SoftShared.SetControl(this, this.labelState, "检查光猫写入信息", Color.BlueViolet);

                    //检查MAC对应的产品信息
                    var deviceType = _listType
                    .Where(a => a.DeviceType == deviceData.DeviceType&&a.PROVINCE==deviceData.HW_PROVINCE_NAME)
                    .FirstOrDefault();

                    if (deviceType == null)
                    {
                        SoftShared.AppendText(this, this.richTextBoxMsg, "获取产品校验信息失败");
                        SoftShared.SetControl(this, this.labelState, "FAIL", Color.Red);
                        CloseTelnet(tel);
                        continue;
                    }

                    if (CheckDeviceMessage(deviceData, deviceType, tel) == false)
                    {
                        SoftShared.AppendText(this, this.richTextBoxMsg, "检查产品信息失败");
                        SoftShared.SetControl(this, this.labelState, "FAIL", Color.Red);
                        CloseTelnet(tel);
                        continue;
                    }
                    else
                    {
                        SoftShared.AppendText(this, this.richTextBoxMsg, "检查产品信息成功");
                    }


                    SoftShared.SetControl(this, this.labelState, "检查温补和校准值", Color.BlueViolet);

                    //检查WIFI校准值和温补值
                    bool checkWifi = SoftShared.CheckWifiMsg(tel, out string msg);

                    SoftShared.AppendText(this, this.richTextBoxMsg, msg);
                    if (checkWifi == false)
                    {
                        SoftShared.SetControl(this, this.labelState, "FAIL", Color.Red);
                        SoftShared.AppendText(this, this.richTextBoxMsg, "检查WIFI校准值和温补值失败");
                        CloseTelnet(tel);
                        continue;
                    }
                    else
                    {
                        //SoftShared.SetControl(this, this.labelState, "WIFI校准和温补值检查成功", Color.Green);
                        SoftShared.AppendText(this, this.richTextBoxMsg, "检查WIFI校准和温补值成功");
                    }

                    //语音按键测试
                    bool testResult = VoiceButtonTest(tel, deviceData);

                    if (testResult == false) { SoftShared.SetControl(this, this.labelState, "FAIL", Color.Red); CloseTelnet(tel); continue; }

                    //恢复出厂模式
                    /**
                     * 写入执行mp_default + reboot -f
                     * */

                    bool resetResult = tel.SendAndRecv("mp_default", "reboot", out msg, 8000);

                    SoftShared.AppendText(this, this.richTextBoxMsg, msg + "\r测试完毕请等待5秒，重新启动光猫");

                    if (resetResult) SoftShared.SetControl(this, this.labelState, "PASS", Color.Green);
                    else SoftShared.SetControl(this, this.labelState, "恢复出厂失败", Color.Red);

                    CloseTelnet(tel);
                    //MessageBox.Show("测试完成");
                }
            });
        }

        private bool CheckDeviceMessage(Device_Data deviceData, Device_Type deviceType, YcdTelnetSecond tel)
        {
            string msg = string.Empty;

            //固件版本检查
            var writeResult = tel.SendAndRecv("cat /etc/swver", deviceType.BuildTime, out msg);
            if (writeResult == false) { SetResultOne("校验编译时间失败", msg); }
            else { SetResultOne("校验编译时间成功", msg); }

            //获取固件类型
            if (deviceData.DeviceType.Contains("CMCC"))//移动
            {
                _modemType = ModemType.YD_924;
            }
            else if (deviceData.DeviceType.Contains("PT952"))//联通
            {
                _modemType = ModemType.LT_952;

            }
            else if (deviceData.DeviceType.Contains("PT926"))//电信9603C
            {
                _modemType = ModemType.DX_926;
            }
            else if (deviceData.DeviceType.Contains("521"))//电信9603C
            {
                _modemType = ModemType.DX_101;
            }
            else//电信PT924G/PT924
            {
                _modemType = ModemType.DX_924;
            }

            //设备序列号
            writeResult = tel.SendAndRecv("flash get HW_SERIAL_NO", "=" + deviceData.OUI2, out msg);
            if (writeResult == false) { SetResultOne("检查设备序列号失败", msg); return false; }
            else { SetResultOne("检查设备序列号成功", msg); }

            //设备型号
            //ADD_18.3.20，兼容新的类型
            string type = deviceData.DeviceType.Replace(" ", "");
            if (type.Contains("_"))
            {
                type = type.Substring(0, type.IndexOf("_"));
            }

            writeResult = tel.SendAndRecv("flash get HW_CWMP_PRODUCTCLASS", "=" + type, out msg);
            if (writeResult == false) { SetResultOne("检查设备型号失败", msg); return false; }
            else { SetResultOne("检查设备型号成功", msg); }


            //XPON型号
            string ponMode = null;
            if (deviceData.PONType == "STIA")
            {
                ponMode = "1";
            }
            else
            {
                ponMode = "2";
            }

            //writeResult = WriteAndReadMsg(tel, string.Format("flash get PON_MODE", ponMode), "flash get PON_MODE", "=" + ponMode, "=" + ponMode);
            writeResult = tel.SendAndRecv("flash get PON_MODE", "=" + ponMode, out msg);
            if (writeResult == false) { SetResultOne("检查PON的型号失败", msg); return false; }
            else { SetResultOne("检查PON的型号成功", msg); }

            //硬件版本
            //writeResult = WriteAndReadMsg(tel, string.Format("flash get HW_HWVER", deviceType.HWVersion), "flash get HW_HWVER", "=" + deviceType.HWVersion, "=" + deviceType.HWVersion);
            writeResult = tel.SendAndRecv("flash get HW_HWVER", "=" + deviceType.HWVersion, out msg);
            if (writeResult == false) { SetResultOne("检查设备硬件版本失败", msg); return false; }
            else { SetResultOne("检查设备硬件版本成功", msg); }

            //普通用户密码
            //writeResult = WriteAndReadMsg(tel, string.Format("flash get DEFAULT_USER_PASSWORD", deviceData.WedPassword), "flash get DEFAULT_USER_PASSWORD", "=" + deviceData.WedPassword, "=" + deviceData.WedPassword);
            writeResult = tel.SendAndRecv("flash get DEFAULT_USER_PASSWORD", "=" + deviceData.WedPassword, out msg);
            if (writeResult == false) { SetResultOne("检查普通用户密码失败", msg); return false; }
            else { SetResultOne("检查普通用户密码成功", msg); }

            //无线名称
            //writeResult = WriteAndReadMsg(tel, string.Format("flash get DEFAULT_WLAN_SSID", deviceData.WIFI_SSID), "flash get DEFAULT_WLAN_SSID", "=" + deviceData.WIFI_SSID, "=" + deviceData.WIFI_SSID);
            writeResult = tel.SendAndRecv("flash get DEFAULT_WLAN_SSID", "=" + deviceData.WIFI_SSID, out msg);
            if (writeResult == false) { SetResultOne("检查WIFI名称失败", msg); return false; }
            else { SetResultOne("检查WIFI名称成功", msg); }

            //无线密钥
            //writeResult = WriteAndReadMsg(tel, string.Format("flash get DEFAULT_WLAN_WPAKEY", deviceData.WIFI_Password), "flash get DEFAULT_WLAN_WPAKEY", "=" + deviceData.WIFI_Password, "=" + deviceData.WIFI_Password);
            writeResult = tel.SendAndRecv("flash get DEFAULT_WLAN_WPAKEY", "=" + deviceData.WIFI_Password, out msg);
            if (writeResult == false) { SetResultOne("检查WIFI密码失败", msg); return false; }
            else { SetResultOne("检查WIFI密码成功", msg); }

            //非联通检查，itv_ssid不为空要检查
            if (string.IsNullOrEmpty(deviceData.ITV_SSID) == false)
            {
                //ITV名称
                writeResult = tel.SendAndRecv("flash get HW_SSID2ND", "=" + deviceData.ITV_SSID, out msg);
                if (writeResult == false) { SetResultOne("检查ITV SSID名称失败", msg); return false; }
                else { SetResultOne("检查ITV SSID成功", msg); }
            }

            if (string.IsNullOrEmpty(deviceData.ITV_Password) == false)
            {
                //ITV密钥
                writeResult = tel.SendAndRecv("flash get HW_WPAKEY2ND", "=" + deviceData.ITV_Password, out msg);
                if (writeResult == false) { SetResultOne("检查ITV 密码失败", msg); return false; }
                else { SetResultOne("检查ITV 密码成功", msg); }
            }

            //光模块型号
            //writeResult = WriteAndReadMsg(tel, string.Format("flash get HW_LIGHT_MODEL", deviceData.LightModule), "flash get HW_LIGHT_MODEL", "=" + deviceData.LightModule, "=" + deviceData.LightModule);
            //writeResult = tel.SendAndRecv("flash get HW_LIGHT_MODEL", "=" + deviceData.ProductCode, out msg);
            //if (writeResult == false) { SetResultOne("检查光模块型号失败", msg); return false; }
            //else { SetResultOne("检查光模块型号成功", msg); }

            //光模块序列号
            //writeResult = WriteAndReadMsg(tel, string.Format("flash get HW_LIGHT_SN", deviceData.LightModuleSN), "flash get HW_LIGHT_SN", "=" + deviceData.LightModuleSN, "=" + deviceData.LightModuleSN);
            writeResult = tel.SendAndRecv("flash get HW_LIGHT_SN", "=" + deviceData.LightModuleSN, out msg);
            if (writeResult == false) { SetResultOne("检查光模块序列号失败", msg); return false; }
            else { SetResultOne("检查光模块序列号成功", msg); }

            //GPON_SN，只有GPON才检查SN号码
            if (deviceData.PONType == "STIA")
            {
                //writeResult = WriteAndReadMsg(tel, string.Format("flash get GPON_SN", deviceData.XPON_SN), "flash get GPON_SN", "=" + deviceData.XPON_SN, "=" + deviceData.XPON_SN);
                writeResult = tel.SendAndRecv("flash get GPON_SN", "=" + deviceData.XPON_SN, out msg);
                if (writeResult == false) { SetResultOne("检查GPON_SN失败", msg); return false; }
                else { SetResultOne("检查GPON_SN成功", msg); }
            }

            //厂商名称
            //string companyName = "youhua";
            //writeResult = WriteAndReadMsg(tel, string.Format("flash get HW_CWMP_MANUFACTURER", companyName), "flash get HW_CWMP_MANUFACTURER", "=" + companyName, "=" + companyName);
            writeResult = tel.SendAndRecv("flash get HW_CWMP_MANUFACTURER", "=" + deviceType.Manufacturer, out msg);
            if (writeResult == false) { SetResultOne("检查厂商名称失败", msg); return false; }
            else { SetResultOne("检查厂商名称成功", msg); }

            //版本校验码key
            //writeResult = WriteAndReadMsg(tel, string.Format("flash get HW_CIPHER", deviceData.HW_CIPHER), "flash get HW_CIPHER", "=" + deviceData.HW_CIPHER, "=" + deviceData.HW_CIPHER);
            writeResult = tel.SendAndRecv("flash get HW_CIPHER", "=" + deviceData.HW_CIPHER, out msg);
            if (writeResult == false) { SetResultOne("检查版本校验码KEY失败", msg); return false; }
            else { SetResultOne("检查版本校验码KEY成功", msg); }

            //软件版本校验码copyright
            //writeResult = WriteAndReadMsg(tel, string.Format("flash get HW_COPYRIGHT", deviceType.HWCopyright), "flash get HW_COPYRIGHT", "=" + deviceType.HWCopyright, "=" + deviceType.HWCopyright);
            writeResult = tel.SendAndRecv("flash get HW_COPYRIGHT", "=" + deviceType.HWCopyright, out msg);
            if (writeResult == false) { SetResultOne("检查软件版本校验码copyright失败", msg); return false; }
            else { SetResultOne("检查软件版本校验码copyright成功", msg); }

            //检查省份信息
            //writeResult = WriteAndReadMsg(tel, string.Format("flash get HW_PROVINCE_NAME", deviceData.HW_PROVINCE_NAME), "flash get HW_PROVINCE_NAME", "=" + deviceData.HW_PROVINCE_NAME, "=" + deviceData.HW_PROVINCE_NAME);
            writeResult = tel.SendAndRecv("flash get HW_PROVINCE_NAME", "=" + deviceData.HW_PROVINCE_NAME, out msg);
            if (writeResult == false) { SetResultOne("检查省份信息失败", msg); return false; }
            else { SetResultOne("检查省份信息成功", msg); }

            //关闭常发光指令
            //writeResult = WriteAndReadMsg(tel, "flash get USER_FACTORY_BEFORE_RUN 0", null, "USER_FACTORY_BEFORE_RUN=", null);
            writeResult = tel.SendAndRecv("flash get USER_FACTORY_BEFORE_RUN", "USER_FACTORY_BEFORE_RUN=", out msg);
            if (writeResult == false) { SetResultOne("关闭常发光失败", msg); return false; }
            else { SetResultOne("关闭常发光成功", msg); }

            return true;
        }

        private bool VoiceButtonTest(YcdTelnetSecond tel, Device_Data device)
        {
            //USB测试
            bool usbTestResult = SoftShared.UsbTest(tel, this.richTextBoxMsg, this);

            if (usbTestResult == false)
            {
                SoftShared.AppendText(this, this.richTextBoxMsg, "USB测试失败");
                return false;
            }
            else
            {
                SoftShared.SetControl(this, this.labelState, "USB测试成功", Color.BlueViolet);
            }


            //语言测试
            string keyVoice = "chid 0 init";

            SoftShared.SetControl(this, this.labelState, "语音测试", Color.BlueViolet);

            bool voiceTestResult = SoftShared.VoiceTest(tel, this, this.richTextBoxMsg, keyVoice);


            if (voiceTestResult == false)
            {
                SoftShared.AppendText(this, this.richTextBoxMsg, "语音测试失败");

                return false;
            }
            else
            {
                SoftShared.SetControl(this, this.labelState, "语音测试成功", Color.BlueViolet);
                //return true;
            }



            //开启灯光
            bool ret = tel.SendAndRecv("mpctl led on", "#", out string msg);
            SoftShared.AppendText(this, this.richTextBoxMsg, msg+ "\r开启灯光指令已经完成");

            string keywifi = string.Empty;
            string keyreset = string.Empty;
            string keywps = string.Empty;

            string outMsg;
            if (_modemType == ModemType.YD_924)//移动
            {
                ret = tel.SendAndRecv("cat /proc/kmsg & led test start", "#", out outMsg);
                SoftShared.AppendText(this, this.richTextBoxMsg, outMsg);

                keywifi = "wifi button";
                keyreset = "reset button";
                keywps = "wps button";
            }
            else if (_modemType == ModemType.LT_952)//联通
            {
                ret = tel.SendAndRecv("cat /proc/kmsg & led test start", "#", out outMsg);
                SoftShared.AppendText(this, this.richTextBoxMsg, outMsg);

                keywifi = "wifi button";
                keyreset = "reset button";
                keywps = "led button";
            }
            else if (_modemType == ModemType.DX_926 )//电信9603C
            {
                ret = tel.SendAndRecv("cat /proc/kmsg & led test start", "#", out outMsg);
                SoftShared.AppendText(this, this.richTextBoxMsg, outMsg);

                keywifi = "wifi button";
                keyreset = "reset button";
            }
            else if (_modemType == ModemType.DX_101)//电信9603C ITV
            {
                ret = tel.SendAndRecv("cat /proc/kmsg & led test start", "#", out outMsg);
                SoftShared.AppendText(this, this.richTextBoxMsg, outMsg);

                keywifi = "wifi button";
                keyreset = "led button";
            }
            else if (_modemType == ModemType.DX_924)
            {
                ret = tel.SendAndRecv("cat /proc/kmsg & led test start", "#", out outMsg);
                SoftShared.AppendText(this, this.richTextBoxMsg, outMsg);

                keywifi = "<4>wifi";
                keyreset = "do nothing";
            }

            if (ret == false)
            {
                SoftShared.AppendText(this, this.richTextBoxMsg, "未能开启按键测试功能");
                return false;
            }

            //

            bool buttonTestResult = SoftShared.ButtonTest(tel, this, this.richTextBoxMsg, this.labelState, keywifi, keyreset, keywps);


            if (buttonTestResult == false)
            {
                SoftShared.AppendText(this, this.richTextBoxMsg, "按键测试失败");
                return false;
            }
            else
            {
                SoftShared.SetControl(this, this.labelState, "按键测试成功", Color.BlueViolet);
            }

            return true;
        }

        private void SetResultOne(string msg, string telnetMsg)
        {
            SoftShared.AppendText(this, this.richTextBoxMsg, telnetMsg);
            SoftShared.AppendText(this, this.richTextBoxMsg, msg);
        }

        private int portOffset = DateTime.Now.Second;


        //private void LoginCMCC()
        //{
        //    if (portOffset >= 800) portOffset = 0;
        //    portOffset++;

        //    HttpArgs args = new HttpArgs()
        //    {
        //        Host = "192.168.1.1",
        //        Url = "/admin/login.asp",
        //        Port = 80
        //    };

        //    HttpBaseClass.GetHttpMsg(_localIp, _localPort + portOffset++, _serverIp, args.Port, args, "OK", out string oMsg, HttpBaseClass.HttpMethod.GET);
        //    //AppendRichTextBox(oMsg);

        //    string pattern = @"csrfmiddlewaretoken.+/>";
        //    var match = Regex.Match(oMsg, pattern);
        //    if (match.Success == false) return;
        //    string csrftoken = match.Value.Substring(match.Value.IndexOf('=') + 2, 32);

        //    pattern = @"csrftoken.+/>";
        //    var match2 = Regex.Match(oMsg, pattern);
        //    if (match2.Success == false) return;
        //    string wareToken = match2.Value.Substring(match2.Value.IndexOf('=') + 2, 32);

        //    args.Url = "/boaform/admin/formLogin";
        //    args.Host = "192.168.1.1";
        //    args.Body = string.Format("username2=user&psd2=&username1={0}&psd1={1}&csrfmiddlewaretoken={2}&username={0}&psd={1}&csrftoken={3}"
        //        , "CMCCAdmin"
        //        , "aDm8H%MdA"
        //        , wareToken
        //        , csrftoken);


        //    HttpBaseClass.GetHttpMsg(_localIp, _localPort + portOffset++, _serverIp, args.Port, args, "OK", out oMsg, HttpBaseClass.HttpMethod.POST);

        //}

        /// <summary>
        /// 获取网页数据
        /// </summary>
        /// <returns></returns>
        private bool GetModemMessage(string url, string key)
        {
            try
            {
                //使用新的类YcdTelnetClass
                if (portOffset >= 800) portOffset = 0;
                portOffset++;

                int port = int.Parse(url.Substring(url.IndexOf(':') + 1, url.IndexOf('/') - url.IndexOf(':') - 1));


                HttpArgs httpArgs = new HttpArgs
                {
                    Url = url.Substring(url.IndexOf('/')),
                    Host = url.Substring(0, url.IndexOf('/')),
                    Cookie = ""
                };

                string httpMsg;
                bool hasKey = HttpBaseClass.GetHttpMsg(_localIp, _localPort + portOffset, _modemIp, port, httpArgs, key, out httpMsg);


                SoftShared.AppendText(this, this.richTextBoxMsg, httpMsg);

                return hasKey;

            }
            catch (Exception ex)
            {
                SoftShared.AppendText(this, this.richTextBoxMsg, ex.ToString());
                return false;
            }
        }

        private YcdTelnetSecond TelnetModem(Device_Data data)
        {
            if (portOffset >= 800) portOffset = 0;

            portOffset += 5;

            if (string.IsNullOrEmpty(data.DeviceType)) return null;

            YcdTelnetSecond tel = new YcdTelnetSecond(_modemIp, _localIp, _localPort + portOffset);

            bool telRet = false;

            for (int i = 0; i < 5; i++)
            {
                if (telRet = tel.Connect()) break;
                Thread.Sleep(5000);
            }

            if (telRet == false) return null;

            string msg = string.Empty;

            if (data.DeviceType.Contains("CMCC"))
            {
                tel.SendAndRecv("yhtcAdmin", "password", out msg);
                SoftShared.AppendText(this, this.richTextBoxMsg, msg);
                if (telRet == false) { tel.Close(); return null; }
                telRet = tel.SendAndRecv("Cm1@YHfw", "#", out msg);
                if (telRet == false)
                { tel.Close(); return null; }
            }
            else if (data.DeviceType.Contains("PT952"))
            {
                tel.SendAndRecv("yhtcAdmin", "password", out msg);
                SoftShared.AppendText(this, this.richTextBoxMsg, msg);
                if (telRet == false)
                { tel.Close(); return null; }
                telRet = tel.SendAndRecv("Cuc@YHfw", "#", out msg);
                if (telRet == false)
                { tel.Close(); return null; }
            }
            else
            {
                tel.SendAndRecv("admin", "Password", out msg);
                SoftShared.AppendText(this, this.richTextBoxMsg, msg);
                if (telRet == false)
                { tel.Close(); return null; }

                if (data.HW_PROVINCE_NAME.Equals("FJ_926"))//福建的开TELNET密码
                {
                    telRet = tel.SendAndRecv(data.WedPassword.Trim(), "$", out msg);
                }
                else
                {
                    telRet = tel.SendAndRecv("TeleCom_1234", "$", out msg);
                }

                SoftShared.AppendText(this, this.richTextBoxMsg, msg);
                if (telRet == false)
                { tel.Close(); return null; }

                tel.SendAndRecv("su", "Password", out msg);
                SoftShared.AppendText(this, this.richTextBoxMsg, msg);
                if (telRet == false)
                { tel.Close(); return null; }
                telRet = tel.SendAndRecv("TeleCom_" + data.MACAddr.Substring(6).ToLower(), "#", out _);
                if (telRet == false)
                { tel.Close(); return null; }
            }

            return tel;
        }

        private void CloseTelnet(YcdTelnetSecond telnetSecond)
        {
            telnetSecond?.Send("exit");
            telnetSecond?.Close();
        }
    }
}
